<?
// Определяем корневой путь
if(isset($_SERVER['HTTP_HOST'])){ // локалка/denwer
  if ($_SERVER['HTTP_HOST'] == 'neftemagnat.ru' && $_SERVER['SERVER_ADDR'] == '127.0.0.1')
    $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; // Этот путь будет использоваться при работе с localhost
   elseif (($_SERVER['HTTP_HOST'] == 'neftemagnat.ru' || $_SERVER['HTTP_HOST'] == 'www.neftemagnat.ru') && $_SERVER['SERVER_ADDR'] != '127.0.0.1')
   	$DOCUMENT_ROOT = '/home/www/neftemagnat.ru/www';
  else $DOCUMENT_ROOT = '/home/neftemagnat.ru/www';
}
else
  $DOCUMENT_ROOT = ''; // Этот путь будет использоваться при работе отладчика Зенда (для корректной работы нужно внести правки в конфигурационный файл для ABSPATH)
require_once($DOCUMENT_ROOT . '/../neftemagnat_start.php'); // Подключаем файл инициализации
// в файле инициализации обновляем время последнего посещения юзера, делаем доступным объект objUser
// и проверку на залогиненность $objUser->isLoggedIn() return true | false; 


//$headTitle = _("Profile");

$arrUrls = array();
$arrUrls[0]['name'] = _("Profile");
$arrUrls[0]['url'] = W_ABSPATH . 'profile';


/*if (!$objUser->isLoggedIn()){
	$objSession->moveTo(W_ABSPATH, _("You are not logged in")); // вы не авторизованы
}*/


$id = isset($_GET["id"]) ? intval($_GET["id"]) : NULL;
$raz = isset($_GET["raz"]) ? intval($_GET["raz"]) : NULL;
$podraz = isset($_GET["podraz"]) ? intval($_GET["podraz"]) : NULL;
$start = isset($_GET["start"]) ? intval($_GET["start"]) : NULL;
$foto = isset($_GET["foto"]) ? intval($_GET["foto"]) : NULL;
$order = isset($_GET["order"]) ? intval($_GET["order"]) : NULL;
$desc = isset($_GET["desc"]) ? intval($_GET["desc"]) : NULL;

$objDB = Service_Database::instance();

$my_order = array(1 => 'lable', 2 => 'city_id', 3 => 'stats', 4 => 'date');
if ($order) $order = $my_order[$order];
$my_desc = array(1 => ' desc', 2 => '');
if ($desc) $desc = $my_desc[$desc];
if ($raz AND !$podraz AND !$id) {
	$arrBBS = $objDB->selectOne("select id,name from ".DB_PREFIX."bbs_razdel WHERE id=$raz");
	$idbbs = $arrBBS["id"];
	$namebbs = $arrBBS["name"];
	$title=$namebbs;
if (!$title) not_found();
} else if ($raz AND $podraz AND !$id) {
	
	$arrBBS = $objDB->selectOne("select id,idraz,name from ".DB_PREFIX."bbs_podpodrazdel WHERE id=$podraz");
	$idbbs = $arrBBS["id"];
	$idraz = $arrBBS["idraz"];
	$namebbs = $arrBBS["name"];
	
	$arrBBS = $objDB->selectOne("select id,name from ".DB_PREFIX."bbs_razdel WHERE id=$idraz");
	$idbbs2 = $arrBBS["id"];
	$namebbs2 = $arrBBS["name"];
	
	$title=$namebbs.' - '.$namebbs2;
	if (!$title) not_found();
} else if (!$raz AND !$podraz AND $id) {
	$arrBBS = $objDB->selectOne("select id,lable from ".DB_PREFIX."bbs WHERE id=$id");
	$idbbs = $arrBBS["id"];
	$namebbs = $arrBBS["lable"];
	
	$title=$namebbs;
if (!$title) not_found();
} else {
	$title='Доски деловых объявлений';
}
if (!$title) not_found();

$headTitle = $title;



include (TEMPLATE."mainHead.php");
?><div class="content left"> <!--center left start--><?


if (!$raz AND !$id AND !$podraz) {
?>
    <h1><?=$title?></h1>
	<br/><p><a href="/add/">Разместить объявление бесплатно&nbsp;&#187;</a></p>
	<br/><table border="0" width="100%"><tr><td width="50%">
	<? $myi=1;
	
	$arrBBS = $objDB->select("select id,name from ".DB_PREFIX."bbs_razdel ORDER BY name");
	foreach ($arrBBS as $arBBS){
		$idbbs = $arBBS["id"];
		$namebbs = $arBBS["name"];
	?>
<p><b><a href="<?=W_ABSPATH?>bbs/<?=$idbbs?>/"><?=$namebbs?></a></b></p><p>
	<?
	$arrRazBBS = $objDB->select("select id,idraz,name from ".DB_PREFIX."bbs_podrazdel WHERE idraz=$idbbs ORDER BY name");
	foreach ($arrRazBBS as $arRazBBS){
		$idbbs = $arRazBBS["id"];
		$idraz = $arRazBBS["idraz"];
		$nameraz = $arRazBBS["name"];
	?>
<?=$nameraz?>.&nbsp;
<? } ?>
</p></td><? if ($myi % 2 == 0) { ?></tr><tr><? } ?><td>
	<? $myi++; } 
	?>
	
	</td></tr></table><br/>
<? } else if ($raz AND !$id AND !$podraz) { ?>
                	<h1><?=$title?></h1>
	<br/>
	<?
	$arrRazBBS = $objDB->select("select id,idraz,name from ".DB_PREFIX."bbs_podrazdel WHERE idraz=$idbbs ORDER BY name");
	foreach ($arrRazBBS as $arRazBBS){
		$idbbs = $arRazBBS["id"];
		$idraz = $arRazBBS["idraz"];
		$nameraz = $arRazBBS["name"];
	?>
	<h2><?=$nameraz?></h2>
	<table border="0" width="100%"><tr><td width="50%">
	<?php $myi=1;
	$arrBBS = $objDB->select("select id,idpodraz,name from ".DB_PREFIX."bbs_podpodrazdel WHERE idpodraz=$idbbs ORDER BY name");
	foreach ($arrBBS as $arBBS){	
		$idbbs = $arBBS["id"];
		$idraz2 = $arBBS["idpodraz"];
		$namebbs = $arBBS["name"];
	?>
<p><b><a href="<?=W_ABSPATH?>bbs/<?=$idraz2?>/<?=$idbbs?>/"><?=$namebbs?></a></b></p><p>
</p></td><? if ($myi % 2 == 0) { ?></tr><tr><? } ?><td>
	<? $myi++; } ?></td></tr></table><br/>
	<? } ?>
<? } else if ($raz AND !$id AND $podraz) { ?>
	<h1><?=$title?></h1>
<div class="sort">
	<p><form action="<?=W_ABSPATH?>bbs/<?=$raz?>/<?=$podraz?>/" method="get" id="firms" name="firms">
	<select name="order" onChange="firms.submit()">
	<option value="1"<? if ($order == $my_order['1']) print ' selected' ?>>по алфавиту</option>
	<option value="2"<? if ($order == $my_order['2']) print ' selected' ?>>по городу</option>
	<option value="3"<? if ($order == $my_order['3']) print ' selected' ?>>по популярности</option>
	<option value="4"<? if ($order == $my_order['4']) print ' selected' ?>>по актуальности</option>
	</select>
	<select name="desc" onChange="firms.submit()">
	<option value="1"<? if ($desc == $my_desc['1']) print ' selected' ?>>по убыванию</option>
	<option value="2"<? if ($desc == $my_desc['2']) print ' selected' ?>>по возрастанию</option>
	</select>
	<!--input type="submit" value="Отсортировать" -->
	</form></p><br/>
</div>
<div class="news-list">

	<?php
	if (!$start) $start = 0;
	$start1 = 15;
	$count1 = $count = $objDB->selectSimpleField("select count(*) from ".DB_PREFIX."bbs LEFT JOIN ".DB_PREFIX."bbs_types ON ".DB_PREFIX."bbs.id=".DB_PREFIX."bbs_types.idbbs WHERE ".DB_PREFIX."bbs_types.idtype=$podraz");
	if (!$order) $order = 'date desc';
	if (!$desc) $desc = '';
	$arrBBS = $objDB->select("select ".DB_PREFIX."bbs.id,".DB_PREFIX."bbs.iduser,".DB_PREFIX."bbs.date,".DB_PREFIX."bbs.city_id,".DB_PREFIX."bbs.lable,".DB_PREFIX."bbs.stats from ".DB_PREFIX."bbs LEFT JOIN ".DB_PREFIX."bbs_types ON ".DB_PREFIX."bbs.id=".DB_PREFIX."bbs_types.idbbs WHERE ".DB_PREFIX."bbs_types.idtype=$podraz ORDER BY $order $desc LIMIT $start,$start1");
	foreach ($arrBBS as $arBBS){
		$id_com = $arBBS["id"];
		$iduser = $arBBS["iduser"];
		$date_reg = $arBBS["date"];
		$city_id = $arBBS["city_id"];
		$lable = $arBBS["lable"];
		$stats = $arBBS["stats"];
		$arrCity = $objDB->selectOne("select city_id,country_id,region_id,name from city WHERE city_id = $city_id");
		$city_id2 = $arrCity["city_id"];
		$country_id = $arrCity["country_id"];
		$region_id = $arrCity["region_id"];
		$lable_city = $arrCity["name"];
		$arrCountry = $objDB->selectOne("select country_id,name from country WHERE country_id = $country_id");
		$country_id2 = $arrCountry["country_id"];
		$lable_country = $arrCountry["name"];
		preg_match("/^(\d{4})-(\d{2})-(\d{2}).*?$/",$date_reg,$m);
		$mon = $m[2];
	    $date_reg=preg_replace("/^(\d{4})-(\d{2})-(\d{2}).*?$/","$3 $my_mons[$mon]",$date_reg);

	?>
<p><b><a href=<?=W_ABSPATH?>bbs<?=$id_com?>><?=$lable?></a></b></p>
	<p>
	<?=$lable_city?> (<?=$lable_country?>)</p>
          <div class="entry-options">
          	<ul>
              	<li><img src="<?=W_IMAGES?>icon_views.gif" alt="Просмотров" /><?=$stats?></b></li>
              	<li class="last"><img src="<?=W_IMAGES?>icon_time.gif" alt="Опубликовано" /><?=$date_reg?></li>
              </ul>
          </div><br/>
	  <?php }
	?>
</div>
<?
	if ($order == 'lable') $order = 1;
	if ($order == 'city_id') $order = 2;
	if ($order == 'stats') $order = 3;
	if ($order == 'date desc' OR $order == 'date') $order = 4;
	if ($desc == ' desc') $desc = 1;
	if ($desc == '') $desc = 1;
  print "<br/><p>";
  if ($count > $start + $start1) $count = $start + $start1;
  if ($start != 0){
    print '<a href='.W_ABSPATH.'bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start='.($start - $start1).'>Назад&nbsp;</a>';
$c_a = $count/$start1;
preg_match("/(\d+)/", $c_a, $matches1);
$c = $matches1[0];
$s_a = $start/$start1;
preg_match("/(\d+)/", $s_a, $matches2);
$s = $matches2[0];
$s = $s + 1;
$mys = $s;
print '[<a href="/bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start=0" title="В начало">1</a>]';
if ($c > 2) {print '<font size=1>...</font>';}
for ($s; $s < $mys + 3; $s ++) {
$k = $s*$start1;
if ($c > $s) {
print '[';
if ($k != $start/$start1) {
# $k = $k - $start1;
print '<a href='.W_ABSPATH.'bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start='.$k.' title="Страница '.$s.'">';
}
print $s;
if ($k != $start/$start1) {
print '</a>';
}
print ']';
}
}
  }
  if ($count1 > $start + $start1)
  {
$c_a = $count1/$start1;
preg_match("/(\d+)/", $c_a, $matches1);
$c = $matches1[0]+1;
$s_a = $start/$start1;
preg_match("/(\d+)/", $s_a, $matches2);
$s = $matches2[0];
$s = $s + 1;
$mys = $s;
for ($s; $s < $mys + 2; $s ++) {
$k = $s*$start1;
$k = $k - $start1;
if ($s < $c) {
print '[';
if ($k != $start) {
# $k = $k + $start1;
print '<a href='.W_ABSPATH.'bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start='.$k.' title="Страница '.$s.'">';
}
print $s;
if ($k != $start) {
print '</a>';
}
print ']';

}
}
if ($s < $c) {print '<font size=1>...</font>';}
    print '[<a href='.W_ABSPATH.'bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start='.(($c-1)*$start1).' title="В конец">'.$c.'</a>]';
print '<a href='.W_ABSPATH.'bbs/'.$raz.'/'.$podraz.'/?order='.$order.'&desc='.$desc.'&start='.($start + $start1).'>&nbsp;Вперед</a>';
  }
  print "</p><br />";
?>
<? } else if (!$raz AND !$podraz AND $id) { ?>
	<h1><?=$title?></h1>
	<br/>
	<?
	$arrBBS = $objDB->selectOne("select id,iduser,idcom,date,date_pub,type,city_id,lable,description,stats,votes from ".DB_PREFIX."bbs where id='$id'");
	$id_com = $arrBBS["id"];
	$iduser = $arrBBS["iduser"];
	$firm = $arrBBS["idcom"];
	$date_reg = $arrBBS["date"];
	$date_pub = $arrBBS["date_pub"];
	$type = $arrBBS["type"];
	$city_id = $arrBBS["city_id"];
	$lable = $arrBBS["lable"];
	$description = $arrBBS["description"];
	$stats = $arrBBS["stats"];
	$votes = $arrBBS["votes"];
	
	
	 if ($firm) {
		$arrBBS = $objDB->selectOne("select id,lable,address,tel,fax,url from ".DB_PREFIX."catalog WHERE id = $firm");
		$com_id = $arrBBS["id"];
		$com_lable = $arrBBS["lable"];
		$com_address = $arrBBS["address"];
		$com_tel = $arrBBS["tel"];
		$com_fax = $arrBBS["fax"];
		$com_url = $arrBBS["url"];
	}
	$arrCity = $objDB->selectOne("select city_id,country_id,region_id,name from city WHERE city_id = $city_id");
	$city_id2 = $arrCity["city_id"];
	$country_id = $arrCity["country_id"];
	$region_id = $arrCity["region_id"];
	$lable_city = $arrCity["name"];
	$arrCountry = $objDB->selectOne("select country_id,name from country WHERE country_id = $country_id");
	$country_id2 = $arrCountry["country_id"];
	$lable_country = $arrCountry["name"];
	$arrUser = $objDB->selectOne("select id,fio from users WHERE id = $iduser");
	$iduser2 = $arrUser["id"];
	$fiouser = $arrUser["fio"];
	$countdoc = $objDB->selectSimpleField("select count(*) from ".DB_PREFIX."docs WHERE idbbs=$id");
	$countvideo = $objDB->selectSimpleField("select count(*) from ".DB_PREFIX."video WHERE idbbs=$id");
	$countfoto = $objDB->selectSimpleField("select count(*) from ".DB_PREFIX."foto where idbbs=$id");
	preg_match("/^(\d{4})-(\d{2})-(\d{2}).*?$/",$date_reg,$m);
	$mon = $m[2];
	$date_reg=preg_replace("/^(\d{4})-(\d{2})-(\d{2}).*?$/","$3 $my_mons[$mon] $1",$date_reg);
	preg_match("/^(\d{4})-(\d{2})-(\d{2}).*?$/",$date_pub,$m);
	$mon = $m[2];
	$date_pub=preg_replace("/^(\d{4})-(\d{2})-(\d{2}).*?$/","$3 $my_mons[$mon] $1",$date_pub);
	$stats++;
/*	$query_news = "update ".DB_PREFIX."bbs set
		stats = '$stats'
		where id='$id'";
	
	mysql_db_query($dbname,$query_news);*/

	?>
	<div id="hr">
	<p><?=$description?></p>&nbsp;
	</div>
	<? if ($countdoc) { ?>
	<div id="hr">
                    	<p><a href="/doc/<?=$id?>"><b>Документы</b></a></p>
	<p><ul><?
	$arrDocs = $objDB->select("select id,iduser,idbbs,lable,format,stats from ".DB_PREFIX."docs WHERE idbbs=$id ORDER BY id desc LIMIT 0,3");
	foreach ($arrDocs as $arrDoc){
		$id_com = $arrDoc["id"];
		$iduser = $arrDoc["iduser"];
		$idnews = $arrDoc["idbbs"];
		$lable = $arrDoc["lable"];
		$format = $arrDoc["format"];
		$stats = $arrDoc["stats"];
		
		$stats2 = substr($stats,-1);
		$filesize = filesize(ABSPATH."docs/".$id_com.$format);
		$filesize=$filesize/1048576;
		$filesize=round($filesize,2);
	?>
		   <li> <a href=<?=W_ABSPATH?>doc/<?=$id?>/<?=$id_com?> title="<?=$lable?>"><?=$lable?></a> (<?=$format?>, <?=$filesize?> Mb, <?=$stats?> просмотр<?=$my_suffix2[$stats2]?>)
	  <?php } ?></ul></p></div><? } ?>
	<? if ($countvideo) { ?>
	<div id="hr">
                    	<p><a href="<?=W_ABSPATH?>video/<?=$id?>"><b>Видео</b></a></p><ul>
	<? $i=1;
	$arrVideos = $objDB->select("select id,iduser,idbbs,lable,stats from ".DB_PREFIX."video WHERE idbbs=$id ORDER BY id desc LIMIT 0,3");
	foreach ($arrVideos as $arrVideo){
		$id_com = $arrVideo["id"];
		$iduser = $arrVideo["iduser"];
		$idnews = $arrVideo["idbbs"];
		$lable = $arrVideo["lable"];
		$stats = $arrVideo["stats"];
		
		$stats2 = substr($stats,-1);
		$filesize = filesize(ABSPATH."video/".$id_com.'.flv');
		$filesize=$filesize/1048576;
		$filesize=round($filesize,2);
		if (!$lable) $lable = '#'.$i;
	?>
	    <li><a href=<?=W_ABSPATH?>video/<?=$id?>/<?=$id_com?> title="<?=$lable?>"><?=$lable?></a> (<?=$filesize?> Mb, <?=$stats?> просмотр<?=$my_suffix2[$stats2]?>)<br/>
	  <?php $i++; } ?></ul></div><? } ?>
	<? if ($countfoto) { ?>
	<div id="hr">
                    	<p><a href="<?=W_ABSPATH?>foto/<?=$id?>"><b>Фото</b></a></p><p>
	<?
 	$arrPhotos = $objDB->select("select id,iduser,lable from ".DB_PREFIX."foto where idbbs=$id ORDER BY id LIMIT 0,8");
	foreach ($arrPhotos as $arrPhoto){
		$idfoto5 = $arrPhoto["id"];
		$iduser5 = $arrPhoto["iduser"];
		$lable_foto5 = $arrPhoto["lable"];
	?>
	<a href=<?=W_ABSPATH?>foto/<?=$id?>/<?=$idfoto5?>><img src="<?=W_IMAGES?>bbs_50/<?=$idfoto5?>.jpg" alt="<?=$lable_foto5?>" border="0" class="border"></a>
	  <?php } ?>&nbsp;</p></div><? } ?>
	<p>
	<? if ($fiouser) { ?>Автор: <b><?=$fiouser?></b><br/><? } ?>
	<?=$lable_city?> (<?=$lable_country?>)<br/>
	<? if ($type) { ?>Тип: <b><?=$my_type[$type]?></b><br/><? } ?>
	<? if (isset($com_id)) { ?><br/>Компания: <b><a href="<?=W_ABSPATH?>firm<?=$com_id?>"><?=$com_lable?></a></b><br/><? } ?>
	<? if (isset($com_tel)) { ?>Тел.: <b><?=$com_tel?></b><br/><? } ?>
	<? if (isset($com_fax)) { ?>Факс: <b><?=$com_fax?></b><br/><? } ?>
	<? if (isset($com_url)) { ?>Сайт: <b><a href="http://<?=$com_url?>" target="_blank"><?=$com_url?></a></b><br/><? } ?>
	<? if (isset($com_address)) { ?>Адрес: <b><?=$com_address?></b><br/><? } ?>
	<? if (isset($date_reg)) { ?>Размещено на сайте: <b><?=$date_reg?></b><br/><? } ?>
	<? if (isset($date_pub)) { ?>Действительно до: <b><?=$date_pub?></b><br/><? } ?>
	Просмотров страницы: <b><?=$stats?></b><br/>
	</p>

<?
	$arrRaz = $objDB->select("select id,idtype,idbbs from ".DB_PREFIX."bbs_types WHERE idbbs=$id");
	foreach ($arrRaz as $arRaz){
		$podraz_id2 = $arRaz["id"];
		$idtype = $arRaz["idtype"];
		$idfirm = $arRaz["idbbs"];
		
		$arrRazBBS = $objDB->selectOne("select id,idraz,name from ".DB_PREFIX."bbs_podpodrazdel WHERE id=$idtype");
		$idbbs2 = $arrRazBBS["id"];
		$idraz2 = $arrRazBBS["idraz"];
		$namebbs2 = $arrRazBBS["name"];
		
		$arrBBS = $objDB->selectOne("select id,name from ".DB_PREFIX."bbs_razdel WHERE id=$idraz2");
		$idbbs3 = $arrBBS["id"];
		$namebbs3 = $arrBBS["name"];
		
		?>
<p><h5><a href="<?=W_ABSPATH?>bbs/">Доски деловых объявлений</a>&nbsp;/&nbsp;<a href="<?=W_ABSPATH?>bbs/<?=$idbbs3?>/"><?=$namebbs3?></a>&nbsp;/&nbsp;<a href="<?=W_ABSPATH?>bbs/<?=$idbbs3?>/<?=$idbbs2?>/"><?=$namebbs2?></a></h5></p>
<? } ?>
<br />

<? } ?>
	</div><!--.content left-->		
<?
include (TEMPLATE."mainBottom.php");
?>